package com.edooteach.tencent.handle.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.http.annotation.Rao;
import com.demo2do.core.utils.JsonUtils;
import com.edooteach.tencent.entity.corpuser.CorpUser;
import com.edooteach.tencent.handle.CorpUserHandler;
import com.edooteach.tencent.rao.CorpUserRao;
/**
 * 
 * @author cuteHU
 *
 */
@Component("corpUserHandler")
public class CorpUserHandlerImpl implements CorpUserHandler {

    private static final Logger logger = LoggerFactory.getLogger(CorpUserHandler.class);
    
    @Rao
    private CorpUserRao corpUserRao;
    
    /*
     * (non-Javadoc)
     * @see com.edooteach.tencent.handle.CorpUserHandler#createUser(com.edooteach.tencent.entity.corpuser.CorpUser)
     */
    public HttpResult createUser(String accessToken, CorpUser corpUserRequest) {
        HttpResult httpResult = corpUserRao.createUser(accessToken, corpUserRequest.generateParams());
        return httpResult;
    }

    /*
     * (non-Javadoc)
     * @see com.edooteach.tencent.handle.CorpUserHandler#deleteUser(java.lang.String)
     */
    public HttpResult deleteUser(String accessToken, String userid) {
        HttpResult httpResult = corpUserRao.deleteUser(accessToken, userid);
        return httpResult;
    }

    /**
     * @param corpUserRequests
     * @return
     */
    private Map<String, Object> generateUserIds(List<String> userids) {
        
        for (String userid : userids) {
          if(StringUtils.isEmpty(userid)) {
            throw BusinessException.newInstance("400", "存在无效UserID异常");
          }
        }
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("useridlist", userids);
        return params;
    }

    /*
     * (non-Javadoc)
     * @see com.edooteach.tencent.handle.CorpUserHandler#deleteUsers(java.util.List)
     */
    public HttpResult deleteUsers(String accessToken, List<String> userids) {
        
        Map<String, Object> params = this.generateUserIds(userids);
        HttpResult httpResult = corpUserRao.batchdeleteUser(accessToken, params);
        
        return httpResult;
    }
    
    

    /*
     * (non-Javadoc)
     * @see com.edooteach.tencent.handle.CorpUserHandler#getDetaildDepartmentUsers(java.lang.Integer)
     */
    public HttpResult getDetaildDepartmentUsers(String accessToken, Integer departmentId) {
        HttpResult httpResult = corpUserRao.simplelistUser(accessToken, departmentId);
        return httpResult;
    }

    /*
     * (non-Javadoc)
     * @see com.edooteach.tencent.handle.CorpUserHandler#loadUser(java.lang.String)
     */
    public HttpResult loadUser(String accessToken, String userid) {
        
        if (!this.validateUserid(userid)) {
          throw BusinessException.newInstance("400", "存在无效UserID异常");
        }
        
        HttpResult response = corpUserRao.loadUser(accessToken, userid);
        logger.info("#loadUser get response [{}]", JsonUtils.toJson(response));
        
        if(!response.isValid()) {
          throw BusinessException.newInstance("500", "获取用户信息失败");
        }
        
        return response;
    }
    
    /**
     * @param userid
     * @return
     */
    private boolean validateUserid(String userid) {
        return StringUtils.isNotEmpty(userid);
    }

    /*
     * (non-Javadoc)
     * @see com.edooteach.tencent.handle.CorpUserHandler#getDepartmentUsers(java.lang.Integer)
     */
    public HttpResult getDepartmentUsers(String accessToken, Integer departmentId) {
        
        HttpResult httpResult = corpUserRao.simplelistUser(accessToken, departmentId);
        
        return httpResult;
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooteach.tencent.handle.CorpUserHandler#updateUser(com.edooteach.tencent.entity.corpuser.CorpUser)
     */
    @Override
    public HttpResult updateUser(String accessToken, CorpUser corpUserRequest) {
        HttpResult httpResult = corpUserRao.updateUser(accessToken, corpUserRequest.generateParams());
        return httpResult;
    }
    
}
